﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string batch_Add(batch model ,int ACID);


[OperationContract]
string batch_Update(batch model ,int ACID);


[OperationContract]
string DeletebatchByMoreKey(string batchcode,string GoodsCode,int ACID);


[OperationContract]
batch GetbatchByMoreKey(string batchcode,string GoodsCode,int ACID);


[OperationContract]
List<batch> batch_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加batch表数据
/// <summary>
/// 添加batch
/// </summary>
/// <param name="model">batch实体</param>
/// <returns>空或错误</returns>
public string batch_Add(batch model,int ACID)
{
if(string.IsNullOrEmpty(model.batchcode))
{
return "『批次号』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.onhand==null )
{
return "『库存』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.batch.Count(p=>p.batchcode==model.batchcode&&p.GoodsCode==model.GoodsCode)>0)
{
return "主键已存在！";
}
ctx.batch.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加batch表数据事物用重构
/// <summary>
/// 添加batch事物用重构
/// </summary>
/// <param name="model">batch实体</param>
/// <returns>空或错误</returns>
private string batch_Add(batch model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.batchcode))
{
return "『批次号』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.onhand==null )
{
return "『库存』不能为空！";
}
if(ctx.batch.Count(p=>p.batchcode==model.batchcode&&p.GoodsCode==model.GoodsCode)>0)
{
return "主键已存在！";
}
ctx.batch.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改batch表数据
/// <summary>
/// 修改batch
/// </summary>
/// <param name="model">batch实体</param>
/// <returns>空或错误</returns>
public string batch_Update(batch model,int ACID)
{
if(string.IsNullOrEmpty(model.batchcode))
{
return "『批次号』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.onhand==null )
{
return "『库存』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.batch.Count(p=>p.batchcode==model.batchcode&&p.GoodsCode==model.GoodsCode)== 1)
{
return "未找到指定记录或记录不唯一！";
}
batch batchmodel=ctx.batch.Single(p =>p.batchcode==model.batchcode&&p.GoodsCode==model.GoodsCode);
if(!string.IsNullOrEmpty(model.batchcode))
{
batchmodel.batchcode=model.batchcode;
}
if(!string.IsNullOrEmpty(model.GoodsCode))
{
batchmodel.GoodsCode=model.GoodsCode;
}
if(model.produceddate!=null )
{
batchmodel.produceddate=model.produceddate;
}
if(model.validdate!=null )
{
batchmodel.validdate=model.validdate;
}
if(model.onhand!=null )
{
batchmodel.onhand=model.onhand;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除batch表Bybatchcode数据
/// <summary>
/// 删除batchBybatchcode
/// </summary>
/// <param name="batchcode">批次号</param>
/// <param name="GoodsCode">产品编号</param>
/// <returns>空或错误</returns>
public string DeletebatchByMoreKey(string batchcode,string GoodsCode,int ACID)
{
if(string.IsNullOrEmpty(batchcode))
{
return "『批次号』不能为空！";
}
if(string.IsNullOrEmpty(GoodsCode))
{
return "『产品编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.batch.Count(p=>p.batchcode==batchcode&&p.GoodsCode==GoodsCode)!=1)
{
return null;
}
batch model=ctx.batch.Single(p =>p.batchcode==batchcode&&p.GoodsCode==GoodsCode);
ctx.batch.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除batch表Bybatchcode数据[事物用重构]
/// <summary>
/// 删除batchBybatchcode[事物用重构]
/// </summary>
/// <param name="batchcode">批次号</param>
/// <param name="GoodsCode">产品编号</param>
/// <returns>空或错误</returns>
public string DeletebatchByMoreKey(string batchcode,string GoodsCode,DataClassesDataContext ctx,int ACID)
{
if(string.IsNullOrEmpty(batchcode))
{
return "『批次号』不能为空！";
}
if(string.IsNullOrEmpty(GoodsCode))
{
return "『产品编号』不能为空！";
}
if(ctx.batch.Count(p=>p.batchcode==batchcode&&p.GoodsCode==GoodsCode)!=1)
{
return null;
}
batch model=ctx.batch.Single(p =>p.batchcode==batchcode&&p.GoodsCode==GoodsCode);
ctx.batch.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取batch表Bybatchcode数据
/// <summary>
/// 获取batchBybatchcode
/// </summary>
/// <param name="batchcode">批次号</param>
/// <param name="GoodsCode">产品编号</param>
/// <returns>当前表</returns>
public batch GetbatchByMoreKey(string batchcode,string GoodsCode,int ACID)
{
if(string.IsNullOrEmpty(batchcode))
{
return null;
}
if(string.IsNullOrEmpty(GoodsCode))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.batch.Count(p=>p.batchcode==batchcode&&p.GoodsCode==GoodsCode)!=1)
{
return null;
}
return ctx.batch.SingleOrDefault(p=>p.batchcode==batchcode&&p.GoodsCode==GoodsCode);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取batch表数据
/// <summary>
/// 获取batch
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<batch></returns>
public List<batch> batch_Get(Expression<Func<batch, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.batch.ToList();
}
 else 
{
return ctx.batch.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.batch.Where(predicate).ToList();
}
 else 
{
return ctx.batch.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取batch表数据
/// <summary>
/// 获取batch
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<batch></returns>
public List<batch> batch_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return batch_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取batch表数据计数
/// <summary>
/// 获取batch表数据计数
/// </summary>
/// <returns>List<batch></returns>
public int batch_GetCount(Expression<Func<batch, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.batch.Count();

}
 else
{
return ctx.batch.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取batch分页表数据
/// <summary>
/// 获取batch分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<batch></returns>
public List<batch> batch_Getpage(int pageIndex, int pageSize,Expression<Func<batch, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.batch.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.batch.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.batch.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.batch.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改batch表数据
/// <summary>
/// 批量修改batch
/// </summary>
/// <param name="predicate">Expression<Func<batch, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<batch,batch>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int batch_batchUpdate(Expression<Func<batch, bool>> predicate,Expression<Func<batch, batch>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.batch.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改batch表数据事物用重构
/// <summary>
/// 批量修改batch事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<batch, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<batch,batch>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int batch_batchUpdate(Expression<Func<batch, bool>> predicate,Expression<Func<batch, batch>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.batch.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除batch表数据
/// <summary>
/// 批量删除batch
/// </summary>
/// <param name="predicate">Expression<Func<batch, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int batch_batchDel(Expression<Func<batch, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.batch.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除batch表数据事物用重构
/// <summary>
/// 批量删除batch事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<batch, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int batch_batchDel(Expression<Func<batch, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.batch.Delete(predicate);
return ok;
}
#endregion


}
}

